package com.alcj.controller;

import com.alcj.common.constant.SysConstant;
import com.alcj.common.framework.PageController;
import com.alcj.common.utils.CmnUtil;
import com.alcj.model.*;
import com.alcj.service.*;
import com.alibaba.fastjson.JSONObject;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author :wangpf
 * @version :v1.0
 * @Package :com.alcj.controller
 * @Description :${TODO}(检测站查询)
 * @date :2017/9/25 0005 上午 11:10
 */
@Controller
@RequestMapping("/stationDetail")
public class StationDetailController extends PageController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    StationService stationService;

    @Autowired
    CostService costService;

    @Autowired
    ScheduleService scheduleService;

    @Autowired
    ReviewService reviewService;

    @Autowired
    VehicleTypeService vehicleTypeService;

    @RequestMapping("/init")
    public String getStationDetail(HttpServletRequest request, Model model) {

        try{
            // 获取检测站ID
            String  stationId = request.getParameter("stationId");

            // 车辆类型小型车车辆类型ID
            String smallCarId = "";
            // 车辆类型大型车车辆类型ID
            String overSizeVehicleId = "";
            // 评价条数
            Integer reviewCount = 0;
            // 总分数
            Integer sumGrade = 0;
            // 差评数
            Integer positiveCount = 0;
            // 中评数
            Integer moderateCount = 0;
            // 好评数
            Integer negativeCount = 0;

            // 获取车辆类型列表
            List<VehicleType> vehicleTypeList = vehicleTypeService.selectVehicleTypeList();

            ReviewDto review = new ReviewDto();
            review.setStationid(stationId);

            // 根据检测站ID获取检测站信息
            Station stationDetail = stationService.selectStationDetailByStationId(stationId);
            // 根据检测站ID获取该检测站可检的车辆类型以及费用
            List<Cost> costList = costService.selectCostListByStationId(stationId);
            // 根据检测站ID获取该检测站的客户评价
            List<ReviewDto> reviewDtoList = reviewService.selectReviewDtoListByStationId(stationId);

            reviewCount = reviewDtoList.size();

            // 循环车辆类型列表
            for(VehicleType vehicleType : vehicleTypeList){

                // 获取小型车的车辆类型ID
                if(vehicleType.getVehicletypename().equals(SysConstant.VEHICLE_TYPE_SMALL)){
                    smallCarId = vehicleType.getVehicletypeid();
                }
                // 获取大型车的车辆类型ID
                if(vehicleType.getVehicletypename().equals(SysConstant.VEHICLE_TYPE_BIG)){
                    overSizeVehicleId = vehicleType.getVehicletypeid();
                }
            }

            for (ReviewDto reviewDto : reviewDtoList){

                sumGrade += reviewDto.getGrade();

                if (reviewDto.getGrade() == 0 || reviewDto.getGrade() == 1){
                    positiveCount += 1;
                }else if (reviewDto.getGrade() == 2 || reviewDto.getGrade() == 3){
                    moderateCount += 1;
                }else{
                    negativeCount += 1;
                }
            }

            // 总体评价分数
            Double reviewGrade = 0.0;
            if (reviewCount != 0){
                reviewGrade = Math.round(Double.parseDouble(sumGrade.toString())/Double.parseDouble(reviewCount.toString()) * 10) * 0.1d;
            }

            // 车辆类型：小型车显示FLG(1:不存在)
            String smallCarFlg = SysConstant.VEHICLE_TYPE_NO;
//            // 车辆类型：中型车FLG(1:不存在)
//            String midSizeCarFlg = SysConstant.VEHICLE_TYPE_NO;
            // 车辆类型：大型车FLG(1:不存在)
            String overSizeVehicleFlg = SysConstant.VEHICLE_TYPE_NO;
            // 小型车收费
            String smallCarCost = "0";
//            //
//            String midSizeCarCost = "0";
            // 大型车收费
            String overSizeVehicleCost = "0";

            // 循环车辆类型列表
            for(Cost cost : costList){

                // 列表中包含小型车
                if(cost.getVehicletypeid().equals(smallCarId)){
                    smallCarFlg = SysConstant.VEHICLE_TYPE_YES;
                    smallCarCost = cost.getCost().toString();
                }
//                // 列表中包含中型车
//                if(cost.getVehicletypeid().equals("0002")){
//                    midSizeCarFlg = SysConstant.VEHICLE_TYPE_YES;
//                    midSizeCarCost = cost.getCost().toString();
//                }
                // 列表中包含大型车
                if(cost.getVehicletypeid().equals(overSizeVehicleId)){
                    overSizeVehicleFlg = SysConstant.VEHICLE_TYPE_YES;
                    overSizeVehicleCost = cost.getCost().toString();
                }
            }

            model.addAttribute("stationId",stationId);

            model.addAttribute("stationDetail",stationDetail);
            // 小型车
            model.addAttribute("smallCarFlg",smallCarFlg);
//            // 中型车
//            model.addAttribute("midSizeCarFlg",midSizeCarFlg);
            // 大型车
            model.addAttribute("overSizeVehicleFlg",overSizeVehicleFlg);

            // 小型车ID
            model.addAttribute("smallCarId",smallCarId);
            // 大型车ID
            model.addAttribute("overSizeVehicleId",overSizeVehicleId);
            // 小型车收费
            model.addAttribute("smallCarCost",smallCarCost);
//            // 中型车
//            model.addAttribute("midSizeCarCost",midSizeCarCost);
            // 大型车收费
            model.addAttribute("overSizeVehicleCost",overSizeVehicleCost);
            // 服务评价列表
            model.addAttribute("reviewDtoList",reviewDtoList);
            // 服务评价条数
            model.addAttribute("reviewCount",reviewCount);
            // 差评数
            model.addAttribute("positiveCount",positiveCount);
            // 中评数
            model.addAttribute("moderateCount",moderateCount);
            // 好评数
            model.addAttribute("negativeCount",negativeCount);
            // 总体评价分数
            model.addAttribute("reviewGrade",reviewGrade);

            // 获取页数
            String currentPage = "1";
            super.setCurrentPage(Integer.parseInt(currentPage));
            // 设置总件数数
           // int total = reviewService.getReviewCount(stationId);
            super.giveTotalNum(reviewCount);
            if(reviewCount > 0){
                // 设置分页参数
                super.preparePageParams(review);
                // 获取用户评价列表
                List<ReviewDto> reviewList = reviewService.getReviewListForPage(review);

                model.addAttribute("reviewList", reviewList);
            }
            model.addAttribute("j_page", getPage());

        }
        catch(Exception e){

            // 异常报错
            logger.error("获取首页信息异常 : {}", e.getMessage());
            logger.error("异常详细信息 :",e);
            return SysConstant.ERROR_PAGE_500;

        }
        return "stationDetail";
    }

    /**
     * @author :wangpf
     * @version :v1.0
     * @Package :com.alcj.controller
     * @Description :${TODO}(检测站查询)
     * @date :2017/9/25 0005 上午 11:10
     */
    @RequestMapping("/getResidualNumber")
    @ResponseBody
    public JSONObject getResidualNumber(HttpServletRequest request) {

        JSONObject resultJson = new JSONObject();

        try{
            // 检测站ID
            String stationId = request.getParameter("stationId");
            // 车辆类型ID
            String vehicleTypeId = request.getParameter("vehicleTypeId");
            // 预约日期
            String reservationDay = request.getParameter("reservationDay");
            // 预约开始时间
            String reservationStartTime = request.getParameter("reservationStartTime");
            // 预约结束时间
            String reservationEndTime = request.getParameter("reservationEndTime");

            Map residualNumberSearchCondition = new HashMap();
            residualNumberSearchCondition.put("stationId",stationId);
            residualNumberSearchCondition.put("vehicleTypeId",vehicleTypeId);
            residualNumberSearchCondition.put("reservationDay",reservationDay);
            residualNumberSearchCondition.put("reservationStartTime",reservationStartTime);
            residualNumberSearchCondition.put("reservationEndTime",reservationEndTime);

            // 获取可预约数
            Integer residualNumber = scheduleService.getResidualNumber(residualNumberSearchCondition);

            // 根据城市ID获取该城市的汽车检测站列表
            //List<Station> stationList = stationService.selectStationList(cityCode);
            // 检测站列表存在时
            if (!CmnUtil.isEmpty(residualNumber)){
                resultJson.put("status","0");
                resultJson.put("msg","操作成功");
                resultJson.put("data", residualNumber);

            }else{
                resultJson.put("status","1");
                resultJson.put("msg","获取预约数量失败！");
                resultJson.put("data","0");
            }

        }catch(Exception e){
            // 异常报错
            logger.error("获取可预约数异常 : {}", e.getMessage());
            logger.error("异常详细信息 :",e);
            resultJson.put("status","1");
            resultJson.put("msg","获取参数错误！");
            return  resultJson;
        }
        return resultJson;
    }
}
